define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'task_template/index',
                    add_url: 'task_template/add',
                    del_url: 'task_template/del',
                    edit_url: 'task_template/edit',
                    multi_url: 'task_template/multi',
                    dragsort_url: 'task_template/weigh',
                    table: 'task_template',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true},
                        {field: 'template_title', title: __('标题'),operate: 'LIKE'},
                        {field: 'category_name', title: __('类目名称'),operate: 'LIKE'},
                        {field: 'type', title: __('模版类型'), formatter: Table.api.formatter.status, searchList: { '1': '通用模版','2':'类目模版'}},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: { '1': '正常','-1':'已禁用'}},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                            {
                                name: '发布模版任务',
                                text: __('发布模版任务'),
                                extend:'data-area=["70%","90%"]',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'task_template/send_task'
                            }, ]
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(".btn-add").data("area",["70%","90%"]);
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["70%","90%"]);
            })
        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
            


        },
        edit: function () {
            Controller.api.bindevent();
        },
        send_task: function () {
            Controller.api.bindevent();
          
            $(".product").on("click", function() {
                var that = this;
                layer.open({
                    type: 2,
                    title: '标题',
                    maxmin: true,
                    shadeClose: false, // 点击遮罩关闭层
                    area: ['800px', '620px'], //子页面大小
                    btn: ['确定上传'],
                    btnAlign: 'c',
                    content: 'product_add', // iframe的url
                    yes: function (index, layero) { //按钮【按钮一】的回调
                        
                        var body = layer.getChildFrame('body', index);
                        // 根据formId获取form表单数据
                        var formdata = body.find("#fromId").serializeArray();
                        Fast.api.ajax({
                            url: 'distribute_task/product_add',
                            data: formdata,
                        }, function (data, res) {
                            if(res.code == 1){
                                $('.product_id').val(res.data);
                                $('.title').html(formdata[1].value);
                                layer.close(index);
                            }
                        });

                    }
                })
            });

            $('#task_type').change(function() {
                var selectedValue = $('#task_type option:selected').val();
                if(selectedValue == 1){
                    $('.titel').css('display','block')
                }else  if(selectedValue == 2){
                    $('.titel').css('display','none')
                }
            })
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
